package com.ycsoft.ttctn;

// 定义永昌的工具类.
public class YcUtils {
    /* 对一个字符串进行左填充至指定长度的操作。 */
    public static String padLeft(String s, int len, char c, boolean isTrim) {
        String result = "";

        if (s.length() > len) {
            if (isTrim) result = s.substring(0, len);
        } else {
            StringBuffer sbuf = new StringBuffer();

            for(int i = 0; i<(len - s.length()); i++)  sbuf.append(c);
            sbuf.append(s);

            result = sbuf.toString();
        }

        return result;
    }

    public static  String padLeft(String s, int len, char c) {
        return padLeft(s, len, c, true);
    }

    public static  String padLeft(String s, int len) {
        return padLeft(s, len, '0', true);
    }

    /* 对一个字符串进行右填充至指定长度的操作 */
    public static String padRight(String s, int len, char c, boolean isTrim) {
        String result = "";

        if (s.length() > len) {
            if (isTrim) result = s.substring(s.length() - len, s.length());
        } else {
            StringBuffer sbuf = new StringBuffer();

            sbuf.append(s);
            for(int i = 0; i<(len - s.length()); i++)  sbuf.append(c);

            result = sbuf.toString();
        }

        return result;
    }

    public static String padRight(String s, int len, char c) {
        return padRight(s, len, c, true);
    }

    public static String padRight(String s, int len) {
        return padRight(s, len, '0', true);
    }
}
